using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;
using LinqModel.Data; // 根据您的实际命名空间调整
using Pomelo.EntityFrameworkCore.MySql.Infrastructure; // 必需
var builder = WebApplication.CreateBuilder(args);

// 添加服务到容器
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();

// 配置 Swagger
builder.Services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new OpenApiInfo
    {
        Title = "用户管理 API",
        Version = "v1",
        Description = "基于 MySQL 的用户管理 Web API"
    });
});

// 添加 MySQL 数据库上下文（官方包）
builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseMyql(builder.Configuration.GetConnectionString("DefaultConnection"))); // 注意：UseMySQL 不是 UseMySql

var app = builder.Build();

// 配置 HTTP 请求管道
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "用户管理 API v1");
    });
}

// 自动创建数据库
using (var scope = app.Services.CreateScope())
{
    var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
    dbContext.Database.EnsureCreated();
}

app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();

app.Run();